<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\Tree;

/**
 * 维修状态
 *
 * @icon fa fa-circle-o
 */
class Repair extends Backend
{

    /**
     * Repair模型对象
     * @var \app\admin\model\Repair
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Repair;

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function indexone()
    {
        //echo 1;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $keyValue = $this->request->post('keyValue');
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            //dump($where);
            if($keyValue){
                $list = $this->model
                    ->field("id,name")
                    ->where($where)
                    ->where(['pid'=>0])
                    ->where(['id'=>$keyValue])
                    ->order($sort, $order)
                    ->paginate($limit);
            }else{
            $list = $this->model
                ->field("id,name")
                ->where($where)
                ->where(['pid'=>0])
                ->order($sort, $order)
                ->paginate($limit);
            }
            //echo $this->model->getLastSql();exit;
            $result = array("total" => $list->total(), "list" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    public function indextwo()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            $noKeyField = $this->request->param('noKeyField');
            $keyValue = $this->request->param('keyValue');

            if ($this->request->request('keyField') && $noKeyField != 100) {
                if (empty($keyValue)) {
                    $list = $this->selectpage()->getContent();
                    $list = (array)json_decode($list, true);
                    $list['list'] = array_merge([['id' => 0, 'name' => '无', 'pid' => 0]], $list['list']);
                    return json($list);
                }
                return $this->selectpage();
            }

            $categoryList = collection($this->model->where(function ($query) use ($keyValue) {
                if ($keyValue != '') {
                    $query->where('id', 'IN', $keyValue);
                }
            })->select())->toArray();

            $total = count($categoryList);

            if ($total > 1) {
                $tree = Tree::instance();
                $tree->init($categoryList, 'pid');
                $categoryList = $tree->getTreeList($tree->getTreeArray(0));
            }

            if ($noKeyField == 100) {
                foreach ($categoryList as &$item) {
                    if (isset($item['spacer']) && $item['spacer']) {
                        $item['name'] = str_replace($item['spacer'], '', $item['name']);
                    }
                }
                return json([
                    "total" => $total,
                    "list"  => $categoryList
                ]);
            }

            return json([
                "total" => $total,
                "rows"  => $categoryList
            ]);
        }
        return $this->view->fetch();
    }
    /**
     * 添加
     */
    public function add()
    {
        $params = $this->request->post('row/a');
        if ( $this->request->isPost()) {
            if($this->auth->id){
                if($params['pid']){
                    $type = $this->model->where(['id'=>$params['pid']])->value("type");
                    $uin_add['pid'] = $params['pid'];
                    $uin_add['type'] = $type;
                }else{
                    $uin_add['pid'] = 0;
                    if($params['name'] == '质量问题'){
                        $uin_add['type'] = 1;
                    }elseif($params['name'] == '非质量问题'){
                        $uin_add['type'] = 2;
                    }
                }
                $uin_add['name'] = $params['name'];
                $id = $this->model->insertGetId($uin_add);

                $this->success("添加成功");
            }else{
                $this->token();
            }

        }else{
            return parent::add();
        }
    }

}
